//
//  MineViewController.m
//  digitalCurrency
//
//  Created by sunliang on 2018/1/26.
//  Copyright © 2018年 XinHuoKeJi. All rights reserved.
//

#import "MineViewController.h"
#import "MineNetManager.h"
#import "VersionUpdateModel.h"
#import "CEAccoutSettingVC.h"
#import "CTMainPointVC.h"
#import "CEMineHeaderView.h"
#import "AddFriendViewController.h"
#import "CTAnnouncementVC.h"
#import "CTContactUsVC.h"

#import "MyNodeVC.h"
#import "MyOrderVC.h"
#import "MyKuangJiVC.h"
#import "LoginNetManager.h"

@interface MineViewController (){
//    BOOL _isPush;//防止每次点击进入出现动画
}

@property (nonatomic,strong) VersionUpdateModel *versionModel;
@property (nonatomic,strong) CEMineHeaderView *headerView;

@property (nonatomic, strong) UIButton *exitbtn;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = tBgQianColor;
   
    [self setTableHeaderView];
    
}

- (void)setTableHeaderView {
    
    self.headerView = [[CEMineHeaderView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 260+SafeAreaNavTopHeight)];
    self.headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.headerView];
    
   
    __weak typeof(self)weakself = self;
    self.headerView.addFriendBlock = ^{
        AddFriendViewController *add = [[AddFriendViewController alloc] init];
        add.hidesBottomBarWhenPushed = YES;
        [weakself.navigationController pushViewController:add animated:YES];
    };
    
    
    [self.view addSubview:self.tableView];
    [self headRefreshWithScrollerView:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.layer.cornerRadius = 5.0;
    self.tableView.layer.shadowColor = [UIColor colorWithHEX:@"6E6E6E"].CGColor;
    self.tableView.layer.shadowOffset = CGSizeMake(0, 0);
    self.tableView.layer.shadowOpacity = 0.15;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.frame = CGRectMake(16, 220+SafeAreaNavTopHeight, kWindowW-32, 54*4-1);
    self.tableView.rowHeight = 54;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"minecell"];
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView.tableFooterView addSubview:self.exitbtn];
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 70)];
    [self.view addSubview:footView];
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo(self.tableView.bottom).offset(65);
        make.height.equalTo(70);
    }];
    [footView addSubview:self.exitbtn];
    [_exitbtn setTitle:LocalizationKey(@"Logout") forState:UIControlStateNormal];
}

- (void)exitapp {
    [EasyShowLodingView showLodingText:[[ChangeLanguage bundle] localizedStringForKey:@"logOutTip" value:nil table:@"English"]];
    
    [LoginNetManager LogoutForCompleteHandle:^(id resPonseObj, int code) {
        [EasyShowLodingView hidenLoding];
        if (code) {
            [YLUserInfo logout];
            
            [self showLoginViewController];
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark-下拉刷新数据
- (void)refreshHeaderAction {
    [self updateUserInfo];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

    self.title = LocalizationKey(@"Mine");
   
    if(![YLUserInfo isLogIn]) {
           [self showLoginViewController];
       }
       else {
           [self updateUserInfo];
       }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)updateUserInfo {
    [MineNetManager getUserStarInfo:[YLUserInfo shareUserInfo].ID CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] intValue];
            if (codeInt == 0) {
                [UserDefault setString:tUserName value:[NSString stringWithFormat:@"%@",resPonseObj[@"data"][@"userName"]]];
                [UserDefault setString:tValue value:[NSString stringWithFormat:@"%@",resPonseObj[@"data"][@"value"]]];
                [UserDefault setString:tNode value:[NSString stringWithFormat:@"%@",resPonseObj[@"data"][@"node"]]];
                
                self.headerView.nameLabel.text = [NSString stringWithFormat:@"%@",resPonseObj[@"data"][@"userName"]];
                
                self.headerView.copText.text = [NSString stringWithFormat:@"%@%@",resPonseObj[@"data"][@"value"],LocalizationKey(@"我的邀请码：")];
            }
            [self.tableView reloadData];
        }
    }];
    
    
    self.headerView.nameLabel.text = [NSString stringWithFormat:@"%@",[UserDefault getString:tUserName]];
    
    self.headerView.copText.text = [NSString stringWithFormat:@"%@",[UserDefault getString:tValue]];

    [[self.headerView.copBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        NSArray *array = [self.headerView.copText.text componentsSeparatedByString:@"："];
        pasteboard.string = array.lastObject;
        [self.view makeToast:LocalizationKey(@"copyscuuses") duration:1.5 position:CSToastPositionCenter];
    }];
    
    [self.tableView reloadData];

}

-(NSArray *)getNameArr {
    return @[LocalizationKey(@"Revenue record"),LocalizationKey(@"我的矿机"),LocalizationKey(@"我的节点"),LocalizationKey(@"我的订单"),LocalizationKey(@"Notice center"),LocalizationKey(@"Contactus"),LocalizationKey(@"邀请好友"),LocalizationKey(@"Account setting")];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self getNameArr] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"minecell"];
    cell.selectionStyle   = UITableViewCellSelectionStyleNone;
    cell.backgroundColor  = [UIColor whiteColor];
    cell.accessoryType    = UITableViewCellAccessoryDisclosureIndicator;
    
     UIView *lineView = [[UIView alloc]init];
     [cell.contentView addSubview:lineView];
     [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(16);
           make.right.equalTo(-16);
           make.bottom.equalTo(0);
           make.height.equalTo(0.5);
     }];
    lineView.backgroundColor = tTableViewCellLine;
    
    NSArray *titles = [self getNameArr];
    cell.textLabel.text = titles[indexPath.row];
    cell.textLabel.textColor = tThreeColor;
    cell.textLabel.font = kFont(15);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        CTMainPointVC *acc = [[CTMainPointVC alloc] init];
        acc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:acc animated:YES];
    }
    else if (indexPath.row == 1) {
           MyKuangJiVC *acc = [[MyKuangJiVC alloc] init];
           acc.hidesBottomBarWhenPushed = YES;
           [self.navigationController pushViewController:acc animated:YES];
       }
    else if (indexPath.row == 2) {
           MyNodeVC *acc = [[MyNodeVC alloc] init];
           acc.hidesBottomBarWhenPushed = YES;
           [self.navigationController pushViewController:acc animated:YES];
       }
    else if (indexPath.row == 3) {
           MyOrderVC *acc = [[MyOrderVC alloc] init];
           acc.hidesBottomBarWhenPushed = YES;
           [self.navigationController pushViewController:acc animated:YES];
       }
    else if (indexPath.row == 4) {
        CTAnnouncementVC *acc = [[CTAnnouncementVC alloc] init];
        acc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:acc animated:YES];
    }
    else if (indexPath.row == 5) {
        CTContactUsVC *VC = [[CTContactUsVC alloc]init];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }
    else if (indexPath.row == 6) {
       AddFriendViewController *add = [[AddFriendViewController alloc] init];
        add.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:add animated:YES];
    }
    else if (indexPath.row == 7) {
        CEAccoutSettingVC *acc = [[CEAccoutSettingVC alloc] init];
        acc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:acc animated:YES];
    }
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 54;
//}

- (UIButton *)exitbtn {
    if (!_exitbtn) {
        _exitbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _exitbtn.layer.cornerRadius = 22.0;
        [_exitbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _exitbtn.backgroundColor = [UIColor colorWithHEX:maincolor];
        _exitbtn.titleLabel.font = [UIFont systemFontOfSize:17];
        _exitbtn.frame = CGRectMake(23, 20, SCREEN_WIDTH-46, 44);
        
        [_exitbtn addTarget:self action:@selector(exitapp) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _exitbtn;
}

@end
